package com.lgCore.netty.client;

import com.alibaba.fastjson.JSON;
import com.lgCore.client.workHandler.ClientNettyBootStrapPool;
import com.lgCore.common.ProtocolCore;
import com.lgCore.message.CommonRpcRequest;
import com.lgCore.netty.client.handler.NettyClientHandler;
import com.lgCore.netty.codec.handler.CommonRpcDecoderHandler;
import com.lgCore.netty.codec.handler.CommonRpcEncoderHandler;
import com.lgCore.netty.message.*;
import com.lgCore.netty.server.NettyChannelMap;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class NettyClientBootstrap {

    private int port;
    private String host;
    private SocketChannel socketChannel;

//    public static void main(String[] args) {
//        try {
//            ProtocolCore.kyroClass();
//            new NettyClientBootstrap(9999,"127.0.0.1");
//
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }
    private static final EventExecutorGroup group = new DefaultEventExecutorGroup(20);
    public NettyClientBootstrap(int port, String host) throws InterruptedException {
        this.port = port;
        this.host = host;

        start();
    }
    private void start() throws InterruptedException {
        int threadsNum = Runtime.getRuntime().availableProcessors() * 2;
        EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
        Bootstrap bootstrap=new Bootstrap();
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.SO_KEEPALIVE,true);
        bootstrap.group(eventLoopGroup);
        bootstrap.remoteAddress(host,port);
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                ChannelPipeline pipeline = socketChannel.pipeline();
                pipeline.addLast("decoder", new CommonRpcDecoderHandler());
                pipeline.addLast("encoder", new CommonRpcEncoderHandler());
//                pipeline.addLast(new ObjectEncoder());
//                pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                pipeline.addLast("timeout",new IdleStateHandler(20, 10, 120));
                pipeline.addLast("handler", new NettyClientHandler(threadsNum));

//                socketChannel.pipeline().addLast(new IdleStateHandler(20,10,0));
//                socketChannel.pipeline().addLast(new ObjectEncoder());
//                socketChannel.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
//                socketChannel.pipeline().addLast(new NettyClientHandler(threadsNum));
            }
        });
        ChannelFuture future =bootstrap.connect(host,port).sync();
        if (future.isSuccess()) {
            socketChannel = (SocketChannel)future.channel();
            System.out.println("connect server  成功---------");
            //发送心跳连接
            LoginMsg loginMsg = new LoginMsg();
            System.out.println(JSON.toJSONString(loginMsg));
            socketChannel.writeAndFlush(loginMsg);
            System.out.println("发送成功");
        }
    }
    public SocketChannel getSocketChannel() {
        return socketChannel;
    }

}
